import sys
import os
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QPushButton, QVBoxLayout,
                             QHBoxLayout, QLabel, QLineEdit, QMessageBox, QStackedWidget,
                             QGroupBox, QFormLayout, QDialog, QTabWidget, QTableWidget,
                             QTableWidgetItem, QHeaderView, QComboBox, QTextEdit, QGridLayout)
from PyQt5.QtCore import Qt, pyqtSignal, QSize
from PyQt5.QtGui import QFont, QIcon

# 添加项目根目录到系统路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from database import Database

# 管理员窗口
class AdminWindow(QMainWindow):
    def __init__(self, user):
        super().__init__()
        self.user = user
        self.initUI()
        
    def initUI(self):
        self.setWindowTitle('宿舍管理系统 - 管理员界面')
        self.setGeometry(100, 100, 800, 600)
        
        # 创建中央窗口部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 创建主布局
        main_layout = QVBoxLayout(central_widget)
        
        # 欢迎信息
        # 根据user对象的类型处理显示信息
        if isinstance(self.user, dict):
            name = self.user.get('name') or self.user.get('username')
            role = self.user.get('role')
        elif isinstance(self.user, tuple) and len(self.user) >= 3:
            name = self.user[3] if len(self.user) > 3 and self.user[3] else self.user[1]
            role = self.user[2]
        else:
            name = "用户"
            role = "未知"
            
        welcome_label = QLabel(f'欢迎, {name}! 角色: {role}')
        welcome_label.setAlignment(Qt.AlignCenter)
        welcome_font = QFont()
        welcome_font.setPointSize(12)
        welcome_label.setFont(welcome_font)
        main_layout.addWidget(welcome_label)
        
        # 功能按钮布局
        button_layout = QGridLayout()
        
        # 创建功能按钮 - 管理员可以访问所有功能
        self.dormitory_btn = QPushButton('宿舍信息管理')
        self.dormitory_btn.clicked.connect(self.open_dormitory_management)
        button_layout.addWidget(self.dormitory_btn, 0, 0)
        
        self.student_btn = QPushButton('学生信息管理')
        self.student_btn.clicked.connect(self.open_student_management)
        button_layout.addWidget(self.student_btn, 0, 1)
        
        self.allocation_btn = QPushButton('宿舍分配管理')
        self.allocation_btn.clicked.connect(self.open_dormitory_allocation)
        button_layout.addWidget(self.allocation_btn, 1, 0)
        
        self.maintenance_btn = QPushButton('宿舍维护管理')
        self.maintenance_btn.clicked.connect(self.open_maintenance_management)
        button_layout.addWidget(self.maintenance_btn, 1, 1)
        
        self.user_btn = QPushButton('系统用户管理')
        self.user_btn.clicked.connect(self.open_user_management)
        button_layout.addWidget(self.user_btn, 2, 0)
        
        self.fee_btn = QPushButton('宿舍费用管理')
        self.fee_btn.clicked.connect(self.open_fee_management)
        button_layout.addWidget(self.fee_btn, 2, 1)
        
        # 设置按钮大小
        for i in range(button_layout.count()):
            button_layout.itemAt(i).widget().setMinimumHeight(80)
            button_layout.itemAt(i).widget().setFont(QFont('Arial', 12))
        
        # 添加按钮布局到主布局
        button_group = QGroupBox('功能菜单')
        button_group.setLayout(button_layout)
        main_layout.addWidget(button_group)
        
        # 退出按钮
        self.exit_btn = QPushButton('退出系统')
        self.exit_btn.clicked.connect(self.close)
        main_layout.addWidget(self.exit_btn)
    
    def open_dormitory_management(self):
        """打开宿舍信息管理窗口"""
        from iteration1_dormitory_management.gui_dormitory_management import DormitoryManagementWindow
        self.dormitory_window = DormitoryManagementWindow()
        self.dormitory_window.show()
    
    def open_student_management(self):
        """打开学生信息管理窗口"""
        from iteration2_student_management.gui_student_management import StudentManagementWindow
        self.student_window = StudentManagementWindow()
        self.student_window.show()
    
    def open_dormitory_allocation(self):
        """打开宿舍分配管理窗口"""
        from iteration3_dormitory_allocation.gui_dormitory_allocation import DormitoryAllocationWindow
        self.allocation_window = DormitoryAllocationWindow()
        self.allocation_window.show()
    
    def open_maintenance_management(self):
        """打开宿舍维护管理窗口"""
        from iteration4_maintenance_management.gui_maintenance_management import MaintenanceManagementWindow
        self.maintenance_window = MaintenanceManagementWindow()
        self.maintenance_window.show()
    
    def open_user_management(self):
        """打开系统用户管理窗口"""
        from iteration5_user_management.gui_user_management import UserManagementWindow
        self.user_window = UserManagementWindow()
        self.user_window.show()
    
    def open_fee_management(self):
        """打开宿舍费用管理窗口"""
        from iteration6_fee_management.gui_fee_management import FeeManagementWindow
        self.fee_window = FeeManagementWindow()
        self.fee_window.show()

# 测试代码
if __name__ == "__main__":
    app = QApplication(sys.argv)
    # 创建一个测试用户
    test_user = {'username': 'admin', 'role': 'admin', 'name': '系统管理员'}
    window = AdminWindow(test_user)
    window.show()
    sys.exit(app.exec_())